package com.cssl.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

//@Configuration
public class RabbitMqConfig implements RabbitTemplate.ReturnsCallback, RabbitTemplate.ConfirmCallback {
    public static final String ORDER_GOOD_QUEUE = "order_goods_queue";//订单商品队列
//    public static final String DELAY_QUEUE = "delay_queue";//延时队列
//    public static final String DEAD_QUEUE = "dead_queue";//死信队列
//    public static final String DELAY_EXCHANGE = "delay_exchange";//延迟交换机
    public static final String ORDER_GOOD_EXCHANGE = "order_goods_exchange";//订单商品交换机
//    public static final String DEAD_EXCHANGE = "dead_exchange";//死信交换机
//    public static final String DELAY_ROUTINGKEY = "delay_routingkey";//延时路由
//    public static final String DEAD_ROUTINGKEY = "dead_routingkey";//死信路由
    public static final String ORDER_GOODS_ROUTINGKEY = "order_goods_routingkey";//创建订单商品路由


//    @Bean
//    public Queue delayQueue() {
//        Map<String, Object> map = new HashMap<>();
//        map.put("x-dead-letter-exchange", DEAD_EXCHANGE);    //过期的消息给哪个交换机的名字
//        map.put("x-dead-letter-routing-key", DEAD_ROUTINGKEY);   //死信交换机把消息个哪个个routingkey
//        map.put("x-message-ttl", 24 * 60 * 60 * 1000);    //队列过期时间1天
//        return new Queue(DELAY_QUEUE, true, false, false, map);
//    }

//    @Bean
//    public Exchange delayExchange() {
//        return ExchangeBuilder.directExchange(DELAY_EXCHANGE).durable(true).build();
//    }
//    @Bean
//    public Binding delayBind(){
//        return BindingBuilder.bind(delayExchange()).to(delayExchange()).with(DELAY_ROUTINGKEY).noargs();
//    }
//
//
//    @Bean
//    public Queue deadQueue() {
//        return new Queue(DEAD_QUEUE, true, false, false);
//    }
//
//    @Bean
//    public Exchange deadExchange() {
//        return ExchangeBuilder.directExchange(DEAD_EXCHANGE).durable(true).build();
//    }
//    @Bean
//    public Binding deadBind(){
//        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(DEAD_ROUTINGKEY).noargs();
//    }

    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        System.out.println("returnedMessage = " + returnedMessage);
        //添加到死信
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        if (b) {
            System.out.println("correlationData = " + correlationData.getId());
        } else {
            System.out.println(" failed ");
        }
    }


    @Bean
    public RabbitTemplate rabbitTemplate(CachingConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        //设置给rabbitTemplate
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnsCallback(this);
        //默认中交换机处理失败了是不处理的，也不会执行回调函数。设置为true后交换机处理失败了则会将消息回退到生产者
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }
    //序列化jackson
    @Bean
    public MessageConverter jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    //订单商品
    @Bean
    public Queue orderQueue(){
        return new Queue(ORDER_GOOD_QUEUE,true,false,false);
    }

    @Bean
    public Exchange orderExchange(){
        return ExchangeBuilder.directExchange(ORDER_GOOD_EXCHANGE).durable(true).build();
    }

    @Bean
    public Binding orderBind(){
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ORDER_GOODS_ROUTINGKEY).noargs();
    }
}
